package com.sskj.xgja.login;


import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gyf.barlibrary.ImmersionBar;
import com.just.agentweb.AgentWeb;
import com.sskj.base.Constant;
import com.sskj.base.view.ToolBarLayout;
import com.sskj.xgja.MVPActivity;

import com.sskj.xgja.R;
import com.sskj.xgja.WebViewActivity;
import com.sskj.xgja.login.presenter.WebViewAgressActivityPresenter;
import com.zzhoujay.richtext.RichText;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import butterknife.BindView;


public class WebViewAgressActivity extends MVPActivity<WebViewAgressActivityPresenter> {

    @BindView(R.id.toolbar)
    ToolBarLayout webToolbar;
    @BindView(R.id.webView)
    WebView webView;
    @BindView(R.id.content)
    TextView contentText;
    @BindView(R.id.ivShishang)
    ImageView ivShishang;

    private AgentWeb mAgentWeb;
    private String noticeId;

    @Override
    public int getLayoutId() {
        return R.layout.activity_web_view_agress;
    }

    @Override
    public WebViewAgressActivityPresenter getPresenter() {
        return new WebViewAgressActivityPresenter();
    }

    @Override
    public void initView() {
        noticeId = getIntent().getStringExtra("url");
        webToolbar.setLeftButtonOnClickListener(v -> finish());
        initWeb();
        if (noticeId.equals("reg_agree")) {
            webToolbar.setTitle("注册协议");
            mPresenter.getAgree(noticeId);
            contentText.setVisibility(View.VISIBLE);
        } else if (noticeId.equals("facebook")) {
            contentText.setVisibility(View.GONE);
            webToolbar.setTitle("FaceBook");
            mAgentWeb.getWebCreator().getWebView().loadUrl("https://www.facebook.com/GoCoiner");
        } else if (noticeId.equals("github")) {
            contentText.setVisibility(View.GONE);
            webToolbar.setTitle("GitHub");
            mAgentWeb.getWebCreator().getWebView().loadUrl("https://github.com/GoCoin");
        } else if (noticeId.equals("twitter")) {
            contentText.setVisibility(View.GONE);
            webToolbar.setTitle("Twitter");
            mAgentWeb.getWebCreator().getWebView().loadUrl("https://gocoin.com/twitter");
        }
    }

    @Override
    public void initData() {

    }

    private void initWeb() {
        mAgentWeb = AgentWeb.with(this)//这里需要把 Activity 、 和 Fragment  同时传入
                .setAgentWebParent(webView, new LinearLayout.LayoutParams(-1, -1))
                .useDefaultIndicator(Color.BLUE)// 使用默认进度条
                .createAgentWeb()
                .ready()
                .go("");
    }


    //此方法获取里面的img，设置img的高度100%,固定图片不能左右滑动
    private void imgReset() {
        webView.loadUrl("javascript:(function(){" +
                "var objs = document.getElementsByTagName('img'); " +
                "for(var i=0;i<objs.length;i++)  " +
                "{"
                + "var img = objs[i];   " +
                " img.style.maxWidth = '100%';img.style.height='auto';" +
                "}" +
                "})()");
    }


    /**
     * 开启web页面
     *
     * @param context
     */
    public static void start(Context context, String url) {
        Intent intent = new Intent(context, WebViewAgressActivity.class);
        intent.putExtra("url", url);
        context.startActivity(intent);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }


    @Override
    protected void onPause() {
        mAgentWeb.getWebLifeCycle().onPause();
        super.onPause();

    }

    @Override
    protected void onResume() {
        mAgentWeb.getWebLifeCycle().onResume();
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mAgentWeb.getWebLifeCycle().onDestroy();
    }


    public void loadData(String data) {
        RichText.from(formatImg(data)).into(contentText);
    }

    public String formatImg(String content) {
        Document document = Jsoup.parse(content);
        Elements imgs = document.getElementsByTag("img");
        for (int i = 0; i < imgs.size(); i++) {
            Element img = imgs.get(i);
            img.attr("width", "100%").attr("height", "auto");
            String url = img.attr("src");
            if (!url.contains("http")) {
                url = Constant.SERVER_ADDRESS + url;
            }
            img.attr("src", url);
        }
        return document.toString();
    }
}
